package custcare

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
	"wave-admin/global"
	"wave-admin/model/common/response"
	"wave-admin/model/custcare/request"
	SystemReq "wave-admin/model/system/request"
	"wave-admin/utils"
)

type SysUserApi struct{}

// @Tags SysUser
// @Summary 客服用户列表
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param data body request.UserList true "用户名"
// @Success 200 {string} string "{"code":200,"data":{"list":[],"total":4,"current_page":1,"page_size":20},"msg":"获取用户列表成功！"}"
// @Router /api/v1/sysuser/getUserList [get]
func (a *SysUserApi) GetUserList(ctx *gin.Context) {
	pageInfo := &SystemReq.UserList{}
	if err := ctx.ShouldBindQuery(pageInfo); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if err, list, total := sysUserService.GetUserList(*pageInfo); err != nil {
		global.GnLog.Error("获取客服列表失败!", zap.Error(err))
		response.FailWithMessage(ctx, "获取客服列表失败！")
	} else {
		response.Success(ctx, response.PageResult{
			List:        list,
			Total:       total,
			CurrentPage: pageInfo.CurrentPage,
			PageSize:    pageInfo.PageSize,
		}, "获取客服列表成功！")
	}
}

// @Tags SysUser
// @Summary 设置客服
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param data body request.UpdateSysUser true "是否客服"
// @Success 200 {string} string "{"code":200,"data":null,"msg":"设置客服成功！"}"
// @Router /api/v1/sysuser/user/:id [put]
func (a *SysUserApi) UpdateSysUser(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	user := &request.UpdateSysUser{}
	if err := ctx.ShouldBindJSON(user); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if err := sysUserService.UpdateSysUser(*user, id); err != nil {
		global.GnLog.Error("设置客服失败!", zap.Error(err))
		response.FailWithMessage(ctx, "设置客服失败!")
	} else {
		response.SuccessWithMessage(ctx, "设置客服成功！")
	}
}

func (a *SysUserApi) UpdateAvatar(ctx *gin.Context) {
	userId := utils.GetUserID(ctx)
	if err := sysUserService.UpdateAvatar(userId); err != nil {
		global.GnLog.Error("更新客服头像失败!", zap.Error(err))
		response.FailWithMessage(ctx, "更新客服头像失败!")
	} else {
		response.SuccessWithMessage(ctx, "更新客服头像成功！")
	}
}
